<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2020/05/07
 * Time: 18:17
 */
 
 namespace HiCommon\Repository;

//use HiCommon\Model\AccountSettlement;

class AccountSettlementRepository extends BaseRepository
{
    public function base_model(){
        return new \HiCommon\Model\AccountSettlement();
    }

    public function get_common_list($arr_state = [],$arr_search = [],$arr_option = []){
        $result = self::base_model()
                      ->whereIn('state',$arr_state)
                      ->where(
                        function ($query)
                        use($arr_search) {
                            $n_shop_id = intval($arr_search['shop_id']);
                            if($n_shop_id > 0){
                                $query->where('shop_id',$n_shop_id);
                            }

                            $n_state = intval($arr_search['state']);
                            if($n_state > 0){
                                $query->where('state',$n_state);
                            }
                        })
                      ->commonpage($arr_option);
        return $result;
    }


    /*商户结算-已提现*/
    public function account_order_cash_out_already($n_shop_id = 0){
        $arr_where = [
          'type'=>'order',
          'state'=>3,
        ];
        $result = self::base_model()
                      ->where(
                        function ($query)
                        use($n_shop_id) {
                            $n_shop_id = intval($n_shop_id);
                            if($n_shop_id > 0){
                                $query->where('shop_id',$n_shop_id);
                            }
                        })
                      ->where($arr_where)
                      ->sum('settle_money');
        return $result;
    }

    /*商户结算-提现中*/
    public function account_order_cash_out_ing($n_shop_id = 0){
        $arr_where = [
          'type'=>'order',
          'shop_id'=>$n_shop_id,
        ];
        $result = self::base_model()
                      ->whereIn('state',[1,2])
                      ->where(
                        function ($query)
                        use($n_shop_id) {
                            $n_shop_id = intval($n_shop_id);
                            if($n_shop_id > 0){
                                $query->where('shop_id',$n_shop_id);
                            }
                        })
                      ->where($arr_where)

                      ->sum('settle_money');
        return $result;
    }


    /*商户-结算列表*/
    public function settle_list_for_shop($arr_search = [],$arr_option = []){
        $arr_state = [1,2,3];//显示范围-编辑中、待结算、已结算
        $result = self::get_common_list($arr_state,$arr_search,$arr_option);
        return $result;
    }

    /*商户-订单结算列表*/
    public function settle_order_list_for_shop($arr_search = []){
        $arr_option = [
          'where'=>['type'=>'order']
        ];
        $result = self::settle_list_for_shop($arr_search,$arr_option);
        return $result;
    }

    /*商户-优惠券津贴结算列表*/
    public function settle_coupon_list_for_shop($arr_search = []){
        $arr_option = [
          'where'=>['type'=>'coupon']
        ];
        $result = self::settle_list_for_shop($arr_search,$arr_option);
        return $result;
    }

    /*管理后台-结算列表*/
    public function get_list_for_admin($arr_search = [],$arr_option = []){
        $arr_state = [2,3];//显示范围-待结算、已结算
        $arr_option['with'] = ['shop'];
        $result = self::get_common_list($arr_state,$arr_search,$arr_option);
        return $result;
    }

    /*管理后台-订单结算列表*/
    public function settle_order_list_for_admin($arr_search = []){
        $arr_option = [
          'where'=>['type'=>'order']
        ];
        $result = self::get_list_for_admin($arr_search,$arr_option);
        return $result;
    }

    /*管理后台-优惠券津贴结算列表*/
    public function settle_coupon_list_for_admin($arr_search = []){
        $arr_option = [
          'where'=>['type'=>'coupon']
        ];
        $result = self::get_list_for_admin($arr_search,$arr_option);
        return $result;
    }




}